from selenium.webdriver.support.select import Select

from pages.base_page import BasePage
from selenium.webdriver.common.by import By



class AddCluePage(BasePage):

    url = "/crm/index.php?m=leads&a=add"
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')
    error_locator =(By.XPATH,"/html/body/div/form/fieldset/div[1]")
    put_in_clue_pool_locator = (By.ID, "remove")  # 放入线索池
    owner_name_locator = (By.ID, 'owner_name')  # 负责人
    d_name_locator = (By.ID, 'd_name')  # 选择线索负责人——用户名
    search_d_name_locator = (By.XPATH, '//*[@id="dialog-role-list"]/div/ul/button')  # 搜索按钮
    first_owner_locator = (By.XPATH, '//*[@id="d_content"]/tr/td[1]/input')  # 选择
    ok_locator = (By.XPATH, '/html/body/div[7]/div[3]/div/button[1]')  # OK键
    name_locator = (By.ID, 'name')  # 公司名
    source_locator = (By.ID, 'source')  # 来源
    contacts_name_locator = (By.ID, 'contacts_name')  # 联系人姓名
    position_locator = (By.ID, 'position')  # 职位
    salt_name_locator = (By.ID, 'saltname')  # 尊称
    mobile_locator = (By.ID, 'mobile')  # 手机
    email_locator = (By.ID, 'email')  # 邮箱
    province_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[6]/td[2]/select[1]')  # 省
    city_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[6]/td[2]/select[2]')  # 市
    district_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[6]/td[2]/select[3]')  # 区
    street_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[6]/td[2]/select[3]')  # 街道
    nextstep_time_locator = (By.ID, 'nextstep_time')  # 下次联系时间
    nextstep_locator = (By.ID, 'nextstep')  # 下次联系内容
    description_locator = (By.ID, 'description')  # 备注
    save_locator = (By.XPATH, '//*[@id="form1"]/table/tfoot/tr/td/input[1]')  # 保存
    save_new_locator = (By.CSS_SELECTOR, '#form1 > table > tfoot > tr > td > input:nth-child(2)')  # 保存并新建
    go_back_locator =(By.XPATH, '//*[@id="form1"]/table/tfoot/tr/td/input[3]')   #返回

    def put_in_clue_pool(self):
        """
        点击放入线索池
        """
        self.find_element(self.put_in_clue_pool_locator).click()
    def click_alert(self):
        """
        点击确定提示框
        """
        self.driver.switch_to.alert.accept()



    def click_owner_name(self):
        """
        点击负责人选框
        """
        self.find_element(self.owner_name_locator).click()


    def input_name(self, d_name):
        """
        输入负责人搜索关键词
        """
        self.find_element(self.d_name_locator).send_keys(d_name)

    def click_search_d_name(self):
        """
        搜索负责人——点击搜索按钮
        """
        self.find_element(self.search_d_name_locator).click()

    def click_first_owner(self):
        """
        搜索结果——选择第一个
        """
        self.find_element(self.first_owner_locator).click()

    def click_ok(self):
        """
        选择负责人——点击ok按钮
        """
        self.find_element(self.ok_locator).click()

    def input_company_name(self, company_name):
        """
        输入公司名
        """
        self.find_element(self.name_locator).send_keys(company_name)

    def select_source(self, source_text):
        """
        选择来源
        """
        source_element = Select(self.find_element(self.source_locator))
        source_element.select_by_visible_text(source_text)

    def input_contact_name(self, contacts_name):
        """
        输入联系人姓名
        """
        self.find_element(self.contacts_name_locator).send_keys(contacts_name)

    def input_position(self, position):
        """
        输入职位:
        """
        self.find_element(self.position_locator).send_keys(position)

    def select_salt_name(self, salt_name):
        """
        选择尊称
        """
        salt_element = Select(self.find_element(self.salt_name_locator))
        salt_element.select_by_visible_text(salt_name)

    def input_mobile(self, mobile):
        """
        输入手机
        """
        self.find_element(self.mobile_locator).send_keys(mobile)

    def input_email(self, email):
        """
        输入邮箱
        """
        self.find_element(self.email_locator).send_keys(email)

    def select_province(self, province):
        """
        选择省
        """
        province_element = Select(self.find_element(self.province_locator))
        province_element.select_by_visible_text(province)

    def select_city(self, city):
        """
        选择市
        """
        city_element = Select(self.find_element(self.city_locator))
        city_element.select_by_visible_text(city)

    def select_district(self, district):
        """
        选择区
        """
        district_element = Select(self.find_element(self.district_locator))
        district_element.select_by_visible_text(district)

    def input_street(self, street):
        """
        输入街道
        """
        self.find_element(self.street_locator).send_keys(street)

    def input_nextstep_time(self, nextstep_time):
        """
        输入下次联系时间
        """
        self.find_element(self.nextstep_time_locator).send_keys(nextstep_time)

    def input_nextstep(self, nextstep):
        """
        输入下次联系内容
        """
        self.find_element(self.nextstep_locator).send_keys(nextstep)

    def input_description(self, description):
        """
        输入备注
        """
        self.find_element(self.description_locator).send_keys(description)

    def click_save(self):
        """
        点击保存
        """
        self.find_element(self.save_locator).click()

    def click_save_new(self):
        """
        点击保存新建
        """
        self.find_element(self.save_new_locator).click()

    def click_go_back(self):
        """
        点击返回
        """
        self.find_element(self.go_back_locator).click()

#返回增删改查后的提示信息
    def get_result_tips(self):
        return self.find_element(self.result_locator).text

#返回弹窗文本信息
    def get_alert_text(self):
        return self.driver.switch_to.alert.text

#返回报错弹窗文本信息
    def get_error_text(self):
        return self.find_element(self.error_locator).text

